﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.Mvc;
using MvcHtmlDemo.Models;
using Newtonsoft.Json;


namespace MvcHtmlDemo.Controllers
{

    /// <summary>
    /// 产品
    /// </summary>
    public class ProductController: Controller
    {

        private SqlConnection Connect()
        {
            var connString = WebConfigurationManager.ConnectionStrings["default"].ConnectionString;
            return new SqlConnection(connString);
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 产品列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            var conn = Connect();
            var da = new SqlDataAdapter("select C.Code as CateCode, C.Title as CateTitle, P.* from Product P inner join Category C on P.CateId = C.Id", conn);
            conn.Open();
            var ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            var objString = JsonConvert.SerializeObject(ds.Tables[0]);
            //return Json(ds.Tables[0], JsonRequestBehavior.AllowGet);
            return Content(objString, "application/json");
        }

        /// <summary>
        /// 产品类型数据
        /// </summary>
        /// <returns></returns>
        public ActionResult Cates()
        {
            var conn = Connect();
            var da = new SqlDataAdapter("select Id, Title from Category", conn);
            conn.Open();
            var ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            return Content(JsonConvert.SerializeObject(ds.Tables[0]), "application/json");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var product = new ProductModel();
            var conn = Connect();
            var da = new SqlDataAdapter("select Id, Title from Category", conn);
            var ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            var list = new List<SelectListItem>();
            
            for (var i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(new SelectListItem
                {
                    Value = ds.Tables[0].Rows[i]["Id"].ToString(),
                    Text = ds.Tables[0].Rows[i]["Title"].ToString()
                });
            }
            var select = new SelectList(list);
            ViewBag.Cates = select;
            using (var cmd = new SqlCommand("select * from Product where Id = @id", conn))
            {
                cmd.Parameters.Add(new SqlParameter("@id", id));
             
                using (var reader = cmd.ExecuteReader() )
                {
                    if (reader.Read())
                    {
                        product.Id = reader.GetInt32(0);
                        product.CateId = reader.GetInt32(1);
                        product.Title = reader.GetString(2);
                        product.Code = reader.GetString(3);
                        product.Status = reader.GetInt32(4);
                        if (reader["RegDate"] != DBNull.Value)
                        {
                            product.RegDate = reader.GetDateTime(5);
                        }
                        if (reader["Remark"] != DBNull.Value)
                        {
                            product.Remark = reader.GetString(6);
                        }
                    }
                }
                conn.Close();
            }
            ViewBag.Product = product;
            return View();
            //return View( product);
        }

        [HttpPost]
        public ActionResult Edit(ProductModel model)
        {
            
            return Json(null);
        }

    }
}